package com.arthur.service;

import com.arthur.bean.*;
import com.arthur.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @所在项目:maven_ssm202008_wanghongzhao
 * @所在包:com.arthur.service
 * @作者:arthur_wang
 * @创建时间:2020-08-28 19:34:52
 * @说明:用户模块的业务实现
 **/
@Transactional
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    @Qualifier("userDao")
    private UserDao userDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public int loginByUsernameAndUserpwd(String username, String userpwd) {
        int rows = 0;
        try {
            rows = userDao.loginByUsernameAndUserpwd(username, userpwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int deleteUserinfoById(int id) {
        int rows = 0;
        try {
            rows = userDao.deleteUserinfoById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int deleteCartinfoById(int id) {
        int rows = 0;
        try {
            rows = userDao.deleteCartinfoById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int obtainUserinfoRows() {
        int rows = 0;
        try {
            rows = userDao.obtainUserinfoRows();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public User fetchUserinfoById(int id) {
        User user = null;
        try {
            user = userDao.fetchUserinfoById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int registerByUserinfo(User user) {
        int rows = 0;
        try {
            if(user.getImgpath()==null||user.getImgpath().equals("")){
                user.setImgpath("images/ssm.jpg");
            }
            rows = userDao.registerByUserinfo(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;

    }

    @Override
    public int updateUser(User user) {
        int rows = 0;
        try {
            rows = userDao.updateUser(user);
            System.out.println(rows);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public List<User> fetchUserinfoListBypageno(int pageno) {
        List<User> userList = null;
        try {
            int startIndex = (pageno - 1) * PageBean.ROWS_PERPAGE;
            userList = userDao.fetchUserinfoListByStartindexAndRows(startIndex, PageBean.ROWS_PERPAGE);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public User fetchUserinfoByName(String name) {
        User user = null;
        try {
            user = userDao.fetchUserinfoByName(name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public Product fetchProductinfoByName(String name) {
        Product product = null;
        try {
            product = userDao.fetchProductinfoByName(name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return product;
    }

    @Override
    public int addOrderinfo(Order order) {
        int rows = 0;
        try {
            rows = userDao.addOrderinfo(order);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int addCartinfo(Cart cart) {
        int rows = 0;
        try {
            rows = userDao.addCartinfo(cart);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public int obtainOrderinfoRows() {
        int rows = 0;
        try {
            rows = userDao.obtainOrderinfoRows();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public List<Order> fetchOrderinfoListBypageno(int pageno,String username) {
        List<Order> orderList = null;
        try {
            int startIndex = (pageno - 1) * PageBean.ROWS_PERPAGE;
            orderList = userDao.fetchOrderinfoListByStartindexAndRows(startIndex, PageBean.ROWS_PERPAGE,username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return orderList;
    }

    @Override
    public int obtainCartinfoRows() {
        int rows = 0;
        try {
            rows = userDao.obtainCartinfoRows();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return rows;
    }

    @Override
    public List<Cart> fetchCartinfoListBypageno(int pageno, String username) {
        List<Cart> cartList = null;
        try {
            int startIndex = (pageno - 1) * PageBean.ROWS_PERPAGE;
            cartList = userDao.fetchCartinfoListByStartindexAndRows(startIndex, PageBean.ROWS_PERPAGE,username);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cartList;
    }

    @Override
    public Cart fetchCartById(String oocid) {
        Cart cart=null;
        try {
            int ioocid=Integer.parseInt(oocid);
            cart = userDao.fetchCartinfoByid(ioocid);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return cart;
    }
}
